/**
 * Created by yangxianming on 2018/1/16.
 */

//给文件名上的绑定对象
function initializeData(){
    var trs = document.getElementById("infoTable").getElementsByTagName("tr");
    for (var i = 1; i < trs.length; i++){
        var expect = trs[i].getElementsByTagName("td")[2].innerText;
        var spans = trs[i].getElementsByTagName("td")[3].getElementsByTagName("span");
        trs[i].getElementsByTagName("td")[4].onclick = function(){resultClick(undefined, this.parentNode, undefined)};
        for (var s = 0; s < spans.length; s++){
            var oneSpan = spans[s];
            oneSpan.setAttribute("expect", expect);
            oneSpan.setAttribute("real", "");
            oneSpan.onclick = function(){fileNameClick(this)};
            oneSpan.onmouseover = function(){this.flag=1;fileNameOver(this)};
            oneSpan.onmouseout = function(){this.flag=0}
        }
    }
}

//运行所有用例
function runAll(){
    var lines = document.getElementById("infoTable").getElementsByTagName("tbody")[0].getElementsByTagName('tr');
    //从下标1开始
    resultClick(1, lines, lines.length);
}

//点击文件名生成上传excel并下载，便于收到导入测试
function fileNameClick(obj){
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", "/equipment/downFile?fileName=" + obj.innerText, true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            //td4.innerText = "done";
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apistatus"] == "1"){
                    //下载文件  "/data/equipment/" + obj.innerText
                    var hostName = window.location.hostname;
                    var port = window.location.port;
                    var downloadUrl = "http://" + hostName + ":" + port + "/data/equipment/" + obj.innerText;
                    window.open(downloadUrl);
                }else{
                    //提示错误
                    alert(r["errorMsg"]);
                }

            }else{
                //alert("服务器异常");
            }
        }
    };
    xmlHttp.send();
}

//运行一个用例
function resultClick(fromIndex, lines, endIndex){

    var trObj;
    if (fromIndex != undefined){
        if (fromIndex == endIndex){
            return; //每行都跑过来就跳出来
        }
        trObj = lines[fromIndex];
    }else{
        trObj = lines;
    }

    var uploadUrl = document.getElementById("uploadUrl").value;
    var span = trObj.getElementsByTagName("td")[3].getElementsByTagName("span")[0];  //上传文件名
    var td4 = trObj.getElementsByTagName("td")[4];  //走一个
    td4.innerText = "testing...";

    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", "/equipment/runTest?fileName=" + span.innerText + "&url=" + encodeURIComponent(uploadUrl), true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            td4.innerText = "done";
            if (xmlHttp.status==200){
                span.setAttribute("real", xmlHttp.response);
                if (fromIndex != undefined){
                    resultClick(fromIndex+1, lines, lines.length);
                }
            }else{
                //alert("服务器异常");
            }
        }
    };
    xmlHttp.send();
}



function fileNameOver(obj){
    setTimeout(function(){
        if(obj.flag == 1){
            controlTopDiv("open",obj.innerText , obj.getAttribute("expect"), obj.getAttribute("real"));
        }
    },2000);
}

//隐藏弹窗控制方法
function controlTopDiv(mySwitch, fileName, expect, real){
    var topDiv = document.getElementById('fourDimensionalSpace');
    if (mySwitch == "open") {
        //设置页面抬头
        document.getElementById('topDivTitle').innerText = fileName + " 查看返回值";
        //加载“所有项目”下拉菜单
        //loadSelectList(document.getElementById("topDivProject"), projectList);
        //document.getElementById("topDivProject").options.remove(0);

        document.getElementById('expectResponse').innerText = expect;
        document.getElementById('realResponse').innerText = real;

        //显示页面
        topDiv.style.marginTop = document.documentElement.scrollTop + "px";
        topDiv.style.display = "block";
    }
    if (mySwitch == "close") topDiv.style.display = "none";
}